const Koa = require('koa');
const { port, secret } = require('./config.json');
const Static = require('koa-static');
const Cors = require('koa2-cors');
const Jwt = require('./middlewares/Jwt');
const { koaBody } = require('koa-body');
const router = require('./routers');
const trimMiddleWare = require('./middlewares/trimMiddleWare');
const jwtMiddleWare = require('./middlewares/jwtMiddleWare');
const rightMiddleWare = require('./middlewares/rightsMiddleWare');
const { add } = require('./services/logs');

const app = new Koa();

app.use(Static('static'));  //指定static目录作为静态目录
app.use(Cors());    //添加跨域
app.use(Jwt);     //添加jwt校验
app.use(koaBody({
    multipart: true,
    formidable: {
        maxFileSize: 2 * 1024 * 1024
    }
}));
app.use(trimMiddleWare);
app.use(jwtMiddleWare);
app.use(rightMiddleWare);
app.use(router.routes()).use(router.allowedMethods());

app.on('error', error => {
    add(error + "");
});

app.listen(port, () => {
    console.log(port + '端口已启动成功');
});